package com.wodwl.controller;

import java.io.IOException;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wodwl.configuration.EmailHelper;
import com.wodwl.dao.UserDao;
import com.wodwl.dao.UserDao.Mode;
import com.wodwl.entity.User;

@Controller
public class UserController {
	private String register_view="user/register";
	private String register_success_view="user/register_success";
	private String register_failure_view="user/register_failure";
	private String login_view="user/login";
	private String login_success_view="index/index_login";
	private String login_failure_view="user/login_failure";
	private String info_display="user/user_display";
	private String info_editor="user/user_editor";
	private String list_user="user/user_list";
	

	
	@RequestMapping("/user/logout.do")
	 public ModelAndView logout(HttpServletRequest request,HttpServletResponse response) {
		HttpSession session=request.getSession();
		session.setAttribute("g305_user", null);
		return new ModelAndView(login_view);
		
	}
	@RequestMapping("/user/info_display.do")
	 public ModelAndView displayInfo(HttpServletRequest request,HttpServletResponse response) {
		String request_username=request.getParameter("request_username");//要查询的username
		UserDao dao=new UserDao();
		Map map=new Hashtable();
		User request_user=null;
		if(request_username!=null){
			request_user=dao.selectUserByUsername(request_username);
			map.put("request_user", request_user);
		}
		return new ModelAndView(getInfo_display(),map);
		
	}
	/*
	 * 修改用户信息
	 */
	@RequestMapping("/user/info_editor1.do")
	 public ModelAndView editorInfo1(HttpServletRequest request,HttpServletResponse response) {
		return new ModelAndView(getInfo_editor());
		
	}
	@RequestMapping("/user/info_editor2.do")
	 public ModelAndView editorInfo2(HttpServletRequest request,HttpServletResponse response) {
		User user=new User();
		user.setUsername(request.getParameter("username"));
		user.setRealname(request.getParameter("realname"));
		user.setResearch(request.getParameter("research"));
		user.setTel(request.getParameter("tel"));
		user.setQq(request.getParameter("qq"));
		user.setEmail(request.getParameter("email"));
		user.setWeb(request.getParameter("web"));
		user.setCompany(request.getParameter("company"));
		user.setDepartment(request.getParameter("department"));
		user.setPosition(request.getParameter("position"));
		user.setWeibo(request.getParameter("weibo"));
		user.setLevel(Integer.parseInt(request.getParameter("level")));
		UserDao dao=new UserDao();
		dao.update(user, Mode.UPDATE_By_Username);
		
		HttpSession session=request.getSession();
		session.setAttribute("g305_user", user);
		return new ModelAndView(getInfo_display());
		
	}
	
	/*
	 * 此处不进行校验，校验交给前端
	 * 优点：降低服务器端压力，提高运行速度
	 */
	@RequestMapping("/user/register.do")
    public String regiser(HttpServletRequest request,HttpServletResponse response) {

		String result=null;

		UserDao dao=new UserDao();
	
		User user=new User();
		user.setUsername(request.getParameter("username"));
		user.setPassword(request.getParameter("password"));
		user.setEmail(request.getParameter("email"));
		user.setStatus(User.Status.NotActivated);
		dao.update(user, Mode.INSERT);
		
		EmailHelper.sendHtml(user.getEmail(),user.getUsername());
				
		request.setAttribute("username", user.getUsername());
		request.setAttribute("email", user.getEmail());
		result=getRegister_success_view();

        return result;
    }
	@RequestMapping("/user/activeByEmail.do")
    public String activeByEmail(HttpServletRequest request,HttpServletResponse response) {
		
		UserDao dao=new UserDao();
		dao.updateStatusByEmail(request.getParameter("email"), User.Status.Normal);
		request.setAttribute("username", request.getParameter("username"));
        return "user/active_success";
    }
	
	/*
	 * 检查数据库中是否存在用户，用于注册页面，防止重复
	 */
	@RequestMapping("/user/register_check_user.do")
    public void check_user(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String username=request.getParameter("username");
		UserDao dao=new UserDao();
		boolean flag=dao.checkUserExists(username);
		if(flag)
			response.getWriter().write("true");
		else 
			response.getWriter().write("false");
	}
	@RequestMapping("/user/register_check_email.do")
    public void check_email(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String email=request.getParameter("email");
		UserDao dao=new UserDao();
		boolean flag=dao.checkEmailExists(email);
		if(flag)
			response.getWriter().write("true");
		else 
			response.getWriter().write("false");
	}
	@RequestMapping("/user/login.do")
    public String login(HttpServletRequest request,HttpServletResponse response) {
		String result=null;
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		User param=new User();
		param.setUsername(username);
		param.setPassword(password);
		
		UserDao dao=new UserDao();
		User user=dao.loginbyUsername(param);
		
		if(user!=null){
			HttpSession session=request.getSession();
			session.setAttribute("g305_user", user);
			result=getLogin_success_view();
		}else{
			request.setAttribute("username", username);
			result=getLogin_view();
		}
        return result;
    }
	
	@RequestMapping("/user/login_by_email.do")
    public String loginByEmail(HttpServletRequest request,HttpServletResponse response) {
		String result=null;
		String email=request.getParameter("email");
		String password=request.getParameter("password");

		UserDao dao=new UserDao();
		User user=dao.loginByEmail(email,password);
		
		if(user==null){
			request.setAttribute("email", email);
			request.setAttribute("err_msg","邮箱未注册，去<a href=\"#\">注册</a>。");
			result=getLogin_view();
		}else if(user.getStatus()==User.Status.Normal){//正常
			HttpSession session=request.getSession();
			session.setAttribute("g305_user", user);
			result=getLogin_success_view();
		}else{
			request.setAttribute("username", email);
			request.setAttribute("err_msg", User.Status.StatusMsg.get(user.getStatus()));
			result=getLogin_view();
		}
        return result;
    }
	
	
	@RequestMapping("/user/list_user.do")
	public ModelAndView listUser(HttpServletRequest request){
		HttpSession session=request.getSession();
		User user=(User)session.getAttribute("g305_user");
		UserDao dao=new UserDao();
		List<User> users=dao.listUsers();
		User item=null;
		for(int i=0;i<users.size();i++){
			User temp=users.get(i);
			if(temp.getUsername().equals(user.getUsername())){
				item=temp;
				users.remove(item);
				break;
			}
		}
			
		Map model=new Hashtable();
		model.put("users", users);
		return new ModelAndView(getList_user(),model);
	}
	@RequestMapping("/user/register_view")
	public String getRegister_view() {
		return register_view;
	}
	@RequestMapping("/user/register_success_view.do")
	public String getRegister_success_view() {
		return register_success_view;
	}
	@RequestMapping("/user/register_failure_view.do")
	public String getRegister_failure_view() {
		return register_failure_view;
	}
	@RequestMapping("/user/login_success_view.do")
	public String getLogin_success_view() {
		return login_success_view;
	}
	@RequestMapping("/user/login_failure_view.do")
	public String getLogin_failure_view() {
		return login_failure_view;
	}
	@RequestMapping("/user/login_view.do")
	public String getLogin_view() {
		return login_view;
	}
	public String getInfo_display() {
		return info_display;
	}
	public String getInfo_editor() {
		return info_editor;
	}
	public String getList_user() {
		return list_user;
	}


}
